<?php

namespace app\admin\controller\apply;

use app\common\controller\Backend;
use fast\Random;
use think\Db;

/**
 * 客户管理
 *
 * @icon fa fa-user
 */
class Apply extends Backend
{

    protected $relationSearch = false;

    /**
     * User模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('apply');
    }

    /**
     * 剩余客户
     */
    public function surplus()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {

            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            $time = strtotime(date('Y-m-d'))-3600*24;

            $map['bignumber'] = ['<',Db::raw('neednumber')];
            $map['create_time'] = ['<',$time];



            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->where($where)
                ->where($map)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->where($map)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));

        if ( $this->request->isPost() ){
            $need = $this->request->post('need/d',0);
            if ( !$need ){
                $this->error('参数不能为空');
            }

            $ret = $this->model->where(['id'=>$ids])->update(['neednumber'=>$need,'create_time'=>time(),'update_time'=>time()]);

            if ( $ret ){
                $this->success('更新完成');
            }else{
                $this->error('更新失败');
            }
        }

        $this->assign('row',$row);
        return $this->view->fetch();
    }

    /**
     * 查看详情
     **/
    public function detail()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            $map = [];

            // 判断是不是超级管理员
            if ( !$this->auth->isSuperAdmin() ){
                $map['admin_id'] = $this->auth->id;
            }

            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            $filter = $this->request->get("filter", '');
            $filter = json_decode($filter, TRUE);
            $filter = $filter ? $filter : [];

            if ( !isset($filter['status']) ){
                $map['user.status'] = 'normal';
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->with('group')
                ->where($map)
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->with('group')
                ->where($map)
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $k => $v) {
                $v->hidden(['password', 'salt']);
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 修改状态
    **/
    public function status($ids){

        if ( $ids ){
            if ($this->request->isAjax()){
                $map['id'] = $ids;

                // 判断是不是超级管理员
                if ( !$this->auth->isSuperAdmin() ){
                    $map['admin_id'] = $this->auth->id;
                }

                $ret = $this->model->where($map)->update(['status'=>'hidden']);
                if ( $ret ){
                    $this->success('删除成功');
                }else{
                    $this->success('删除失败');
                }
            }
        }else{
            $this->error('id为空');
        }


    }

}
